package com.FH.permission.privilege_menu;

import com.FH.permission.privilege.PrivilegeDAO;
import com.FH.permission.sys_menu.SysmenuDAO;
import com.FH.permission.sys_menu.SysmenuShowDTO;
import com.FH.permission.sys_role.SysroleDAO;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by acer on 2016/8/4.
 */
@Stateless
public class PrivilegemenuDAOImpl extends AccessDB implements PrivilegemenuDAO {

    @EJB
    private SysmenuDAO sysmenuDAO;
    @EJB
    private PrivilegeDAO privilegeDAO;


    @Override
    public ResultDTO getPrivilegemenuInfo(String search, String page, String size) {

        ResultDTO resultDTO=new ResultDTO();

        try{
            JSONObject json=new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from privilege_menu where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if(page.equals("1")){
                sql_page = " limit 0,"+size+"";
            }else{
                sql_page = " limit "+((page_ok-1)*size_ok+1)+","+size+"";
            }

            if(json.has("sys_menu_id")){
                sql_w += " AND sys_menu_id = "+json.getString("sys_menu_id");
            }
            if(json.has("privilege_menu_id")){
                sql_w += " AND privilege_menu_id = "+json.getString("privilege_menu_id");
            }
            if(json.has("privilege_id")){
                sql_w += " AND privilege_id = '"+json.getString("privilege_id")+"'";

            }
            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String []> seresult = new ArrayList<String []>();

            List<PrivilegemenuShowDTO> result = new ArrayList<PrivilegemenuShowDTO>();
            List<SysmenuShowDTO> sysmenu=new ArrayList<SysmenuShowDTO>();

            if(!existName.equals("0")){

                String sql="select privilege_menu_id,privilege_id,sys_menu_id,sys_menu_create,sys_menu_create_time, " +
                        "sys_menu_modify, sys_menu_modify_time,privilege_menu_note from privilege_menu where 1=1"+sql_w + " ORDER BY menu_order ASC " +sql_page;
                seresult=this.getReportingTo(sql);

                for(int i=0;i<seresult.size();i++){
                    PrivilegemenuShowDTO privilegemenuShowDTO=new PrivilegemenuShowDTO();
                    privilegemenuShowDTO.setPrivilege_menu_id(seresult.get(i)[1]);
                    privilegemenuShowDTO.setPrivilege_id(seresult.get(i)[2]);
                    privilegemenuShowDTO.setSys_menu_id(seresult.get(i)[3]);
                    privilegemenuShowDTO.setSys_menu_create(seresult.get(i)[4]);
                    privilegemenuShowDTO.setSys_menu_create_time(seresult.get(i)[5]);
                    privilegemenuShowDTO.setSys_menu_modify(seresult.get(i)[6]);
                    privilegemenuShowDTO.setSys_menu_modify_time(seresult.get(i)[7]);
                    privilegemenuShowDTO.setPrivilege_menu_note(seresult.get(i)[8]);

//                    for (int j=0;j<result.size();j++){
//                        PrivilegemenuShowDTO ps=result.get(j);
//                        if (ps.getPrivilege_id().equals(seresult.get(i)[2])){
                            String sys_menu_id=seresult.get(i)[3];//获取菜单id
                            ResultDTO sys_menu ;
                            sys_menu=sysmenuDAO.getSysmenuInfo("{sys_menu_id:"+sys_menu_id+",parent_sys_menu_id:\"\"}","1","10000");
                            privilegemenuShowDTO.setSysmenu(sys_menu.getValues());
                            List<SysmenuShowDTO> menu=sys_menu.getValues();
                            sysmenu.add(menu.get(0));
//                        }
//                    }
//                    if(seresult.get(i)[2]!=null){//判断权限id是否存在
//                        String privilege_id=seresult.get(i)[2];//获取权限id
//                        ResultDTO privilege_menu ;
//                        privilege_menu=privilegeDAO.getPrivilegeInfo("{privilege_id:"+privilege_id+"}","1","10000");
//                        privilegemenuShowDTO.setPrivilege(privilege_menu.getValues());
//
//                    }
                    result.add(privilegemenuShowDTO);

                }
            }

            if(seresult.size()!=0) {
                resultDTO.setStatus("90000");
                resultDTO.setValues(sysmenu);
                resultDTO.setDescritpion(warnSrc.get_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO addPrivilegemenuInfo(PrivilegemenuPullDTO privilegemenuPullDTO) {

   //   List<String []> sum = new ArrayList<String []>();//查询返回结果
        String  Sys_menu_ids[]=null;
        String  menu_orders[]=null;

        int resultls=1;//插入条数

        //查询结果
        ResultDTO resultDTO=new ResultDTO();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql_w ="";//查询条件sql组合
        if(privilegemenuPullDTO.getSys_menu_id()!=null){
             String Sys_menu_id=privilegemenuPullDTO.getSys_menu_id();
           // sql_w += " AND sys_menu_id = "+privilegemenuPullDTO.getSys_menu_id();
            //sql_w += " AND sys_menu_id = '" + privilegemenuPullDTO.getSys_menu_id()+"'";
            Sys_menu_ids=Sys_menu_id.split(",");
            menu_orders=privilegemenuPullDTO.getMenu_order().split(",");
        }
        if(privilegemenuPullDTO.getPrivilege_id()!=null){
           // sql_w += " AND privilege_id = "+privilegemenuPullDTO.getPrivilege_id();
            sql_w += " AND privilege_id = '" + privilegemenuPullDTO.getPrivilege_id()+"'";
        }
//        if(privilegemenuPullDTO.getMember_id()!=null){
//            sql_w += " AND member_id = '" + privilegemenuPullDTO.getMember_id()+"'";
//        }


        for (int i=0;i<Sys_menu_ids.length;i++){
            String sys_menu_id=Sys_menu_ids[i];
            String menu_order=menu_orders[i];
            String sql_k = " AND sys_menu_id = '" + sys_menu_id+"'";
            sql_k=sql_k+sql_w;
            String sql="select count(1) from privilege_menu where 1=1"+sql_k;
            String sum=getReportingToOne(sql);
            if(!sum.equals("1")){
                String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");
                String Privilegemenu_insert="";
                Privilegemenu_insert="insert into privilege_menu(privilege_menu_id,privilege_id,sys_menu_id,sys_menu_create,sys_menu_create_time,sys_menu_modify," +
                        "sys_menu_modify_time,privilege_menu_note,menu_order) values ('"+uuid+"','"+privilegemenuPullDTO.getPrivilege_id()+"','"+sys_menu_id+"'," +
                        "'"+privilegemenuPullDTO.getSys_menu_create()+"','"+df.format(new Date())+"','"+privilegemenuPullDTO.getSys_menu_modify()+"'," +
                        "'"+df.format(new Date())+"','"+privilegemenuPullDTO.getPrivilege_menu_note()+"','"+menu_order+"')";
                resultls=addReportingTo(Privilegemenu_insert);
                continue;
            }

        }
        if(resultls!=0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion("菜单分配成功");
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("菜单分配失败");
        }

        return resultDTO;
    }

//    @Override
//    public ResultDTO updatePrivilegemenuInfo(PrivilegemenuPullDTO privilegemenuPullDTO) {
//        ResultDTO resultDTO=new ResultDTO();
//        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");
//
//        return null;
//    }
}

